package com.atguigu.yygh.hosp.testmongo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/mongo2")
public class TestMongo2 {
    @Autowired
    private UserRepository userRepository;


    //添加
    @GetMapping("create")
    public void createUser(){
        User user = new User();
        user.setAge(20);
        user.setName("张三");
        user.setEmail("zhang3@qq.com");
        User user1 = userRepository.save(user);
        System.out.println("user1 = " + user1);
    }

    //查询所有
    @GetMapping("findAll")
    public void  findAll(){
        List<User> list = userRepository.findAll();
        System.out.println("list = " + list);
    }

    //根据id查询
    @GetMapping("findId")
    public void  findId(){
        User user = userRepository.findById("61a5d4507dc35c5061bc860a").get();
        System.out.println("user = " + user);
    }

    //根据条件查询
    @GetMapping("findUser")
    public void  findUser(){
        User user = new User();
        user.setName("张三");
        user.setAge(20);
        Example<User> example = Example.of(user);
        List<User> users = userRepository.findAll(example);
        System.out.println("users = " + users);
    }

    //模糊查询
    @GetMapping("findLike")
    public void  findLike(){
        //创建匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()//构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)//字符串模糊查询匹配规则
                .withIgnoreCase(true);//忽略大小写
        //设置查询条件
        User user = new User();
        user.setName("三");
        //创建查询条件模板
        Example<User> example = Example.of(user,matcher);
        List<User> users = userRepository.findAll(example);
        System.out.println("users = " + users);
    }

    //分页查询
    @GetMapping("findPage")
    public void findUsersPage() {
        Sort sort = Sort.by(Sort.Direction.DESC,"age");
        //分页条件对象,0是第一页
        Pageable pageable = PageRequest.of(0,10,sort);
        //创建匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()//构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)//字符串模糊查询匹配规则
                .withIgnoreCase(true);//忽略大小写
        //设置查询条件
        User user = new User();
        user.setName("三");
        //创建查询条件模板
        Example<User> example = Example.of(user,matcher);
        Page<User> pages = userRepository.findAll(example,pageable);
        System.out.println(pages);
    }


    //修改
    @GetMapping("update")
    public void updateUser() {
        User user = userRepository.findById("61a5d4507dc35c5061bc860a").get();
        user.setName("张三UPUP");
        User save = userRepository.save(user);
        System.out.println("save = " + save);
    }


    //删除
    @GetMapping("delete")
    public void delete() {
        userRepository.deleteById("60b8d57ed539ed5b124942de");
    }

    //根据条件查询
    @GetMapping("findInfo")
    public void  findInfo(){
        List<User> userList = userRepository.getByNameAndAge("张三",20);
        System.out.println("userList = " + userList);
    }
    //根据条件模糊查询
    @GetMapping("findInfoLike")
    public void  findInfoLike(){
        List<User> userList = userRepository.getByNameLike("三");
        System.out.println("userList = " + userList);
    }

}
